class smx_elasticsearch {
  require smx_os
  require smx_download

  Exec {
    path      => [
      '/usr/bin',
      '/bin',
    ],
    logoutput => true,
    timeout   => 0,
  }

  exec { 'extract-elasticsearch':
    creates => "/opt/elasticsearch-1.4.2/bin",
    cwd     => "/tmp/vagrant-cache/wget/elasticsearch",
    command => "unzip elasticsearch.zip -d /opt/",
    before  => File['/opt/elasticsearch'],
  }


  file { '/opt/elasticsearch' :
    ensure => link,
    target => "/opt/elasticsearch-1.4.2",
    before => [ File['/etc/init.d/elasticsearch'], File['/opt/elasticsearch/bin/service/elasticsearch.conf'], File['/opt/elasticsearch/logs'] ],
  }

  file { '/etc/init.d/elasticsearch':
    ensure => '/opt/elasticsearch/bin/service/elasticsearch',
    notify => Service['elasticsearch'],
  }

  file { '/opt/elasticsearch/bin/service/elasticsearch':
    owner  => root, group => root, mode => 755,
    notify => Service['elasticsearch'],
  }


  file { '/opt/elasticsearch/bin/service/elasticsearch.conf' :
    ensure => file,
    owner  => root, group => root, mode => 755,
    source => "puppet:///modules/smx_elasticsearch/elasticsearch.conf",
    notify => Service['elasticsearch'],
  }

  file { '/opt/elasticsearch/config/elasticsearch.yml':
    ensure => file,
    owner  => root, group => root, mode => 755,
    source => "puppet:///modules/smx_elasticsearch/elasticsearch.yml",
    notify => Service['elasticsearch'],
  }

  file { '/opt/elasticsearch/logs':
    ensure => directory,
    owner  => root, group => root, mode => 755,
    notify => Service['elasticsearch'],
  }

  service { 'elasticsearch':
    ensure => running,
    enable => true,
  }
}
